//
//  JOrderDetailController.m
//  HeadIndustry
//
//  Created by jun on 2017/9/11.
//  Copyright © 2017年 jun. All rights reserved.
//

#import "JOrderDetailController.h"
#import "JWaitPayDetailCell.h"
#import "JPayHeadView.h"
#import "JWaitPayDetailButtomView.h"
@interface JOrderDetailController ()

//@property (nonatomic, strong) JWaitPayDetailCell *detailView;
@property (nonatomic, strong) JAddressModel *addressModel;
@property (nonatomic, strong) UIScrollView *scrollView;

@end

@implementation JOrderDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    [super InitUI];
    
    [self configAddressmodel];
    
    [self configTopView];
    
    if (_selType == 0 || _selType == 1) {
        [self setConfigButtomView];
    }
}

- (void)configAddressmodel {
    self.titleLabel.text = @"订单详情";
    self.leftButton.hidden = NO;
    self.rightButton. hidden = YES;
    [self.leftButton setImage:[UIImage imageNamed:backBtnImg] forState:UIControlStateNormal];
    self.leftButton.frame = CGRectMake(8, self.leftButton.frame.origin.y, 30, 30);
    
    _addressModel = [JAddressModel new];
    _addressModel.FirstName = self.waitPayModel.ReceiveName;
    _addressModel.PhoneNumber = self.waitPayModel.ReceiveTel;
    _addressModel.FullAddress = self.waitPayModel.ReceiveAddress;
    
}

- (void)configTopView {
    self.automaticallyAdjustsScrollViewInsets = NO;
    _scrollView = [[UIScrollView alloc]init];
    if (_selType == 0 || _selType == 1) {
        _scrollView.frame = CGRectMake(0, 0, kWindowW, CGRectGetHeight(self.contentView.frame) - 50);
    }
    else
    {
        _scrollView.frame = CGRectMake(0, 0, kWindowW, CGRectGetHeight(self.contentView.frame));
    }
    
    [self.contentView addSubview:_scrollView];
    
    JPayHeadView *addressView = [[JPayHeadView alloc]initWithFrame:CGRectMake(0, 0, kWindowW, 90) isHaveTap:NO];
    addressView.addressModel = self.addressModel;
    [_scrollView addSubview:addressView];
    
    JWaitPayDetailCell *detailView = [[JWaitPayDetailCell alloc]initWithFrame:CGRectMake(0, addressView.bottom + 10 , kWindowW, 200)];
    detailView.selTypt = _selType;
    detailView.waitPayModel = self.waitPayModel;
    [_scrollView addSubview:detailView];
    CGFloat height = [detailView getHeight];
    detailView.frame = CGRectMake(0, addressView.bottom + 10, kWindowW, height);
    _scrollView.contentSize = CGSizeMake(kWindowW, detailView.bottom);
    
    if (_selType == 1) {
        detailView.block = ^(NSInteger num) {
//            NSLog(@"退款");
            [self OrderRebackMethod:num];
        };
    }
}

// 退货
- (void)OrderRebackMethod:(NSInteger)num
{
    NSString *url;
    NSDictionary *dic;
    
    JWaitPayModel *model = self.waitPayModel;
    if ([model.OrderStatus isEqualToString:@"待发货"]) {
        url = @"Orders/ReturnOrderItem"; // 待发货
        JWaitPayDetailModel *model2 = model.OrderItemModels[num];
        dic = @{@"Id":@(model2.Id), //订单明细Id
                @"OrderId":@(model2.OrderId)}; //订单Id


    }
    else if ([model.OrderStatus isEqualToString:@"待收货"]) {
        url = @"Orders/CreateReturnOrder"; // 待收货
    }
    
    [self returnproductRequestUrl:url dic:dic];
    
}


- (void)returnproductRequestUrl:(NSString *)url dic:(NSDictionary *)dic
{
    [JNetManager ReturnOrderRequedtUrl:url
                          waitPatModel:dic
                               success:^(id response) {
                                   if ([response integerValue] == 1) {
                                       [SVProgressHUD showSuccessWithStatus:@"退款成功"];
                                       
                                       [self.navigationController popViewControllerAnimated:YES];
                                       
                                       [[NSNotificationCenter defaultCenter] postNotificationName:OrderRefresh object:nil userInfo:nil];
                                   }
                               } failure:^(NSString *err) {
                                   [SVProgressHUD showErrorWithStatus:err];
                               }];
}

- (void)setConfigButtomView {
    JWaitPayDetailButtomView *view = [[JWaitPayDetailButtomView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.contentView.frame) - 50, kWindowW, 50)];
    if (_selType == 1) {
        if ([self.waitPayModel.OrderStatus isEqualToString:@"待收货"]) {
            view.ishave = YES;
        }
        else
        {
            view.ishave = NO;
        }
    }
    
    view.selType = _selType;
    view.block = ^(NSInteger num) {
        switch (num) {
            case 1:
                NSLog(@"付款");
                break;
            case 2:
                NSLog(@"联系商家");
                break;
            case 3:
//                NSLog(@"取消订单");
                [self cancelOrder];
                
                break;
            case 4:
//                NSLog(@"确认收货");
                [self OrderConfirmMethod];
                break;
                
            default:
                break;
        }
    };
    [self.contentView addSubview:view];
}

// 确认收货
- (void)OrderConfirmMethod
{
    
        self.waitPayModel.OrderStatusId = 40;
        NSString *url = @"Orders/UpdateOrder"; // 确认收货
    
        [JNetManager cancelOrderRequedtUrl:url
                              waitPatModel:self.waitPayModel
                                   success:^(id response) {
                                       if ([response integerValue] == 1) {
                                           [SVProgressHUD showSuccessWithStatus:@"确认收货成功"];
                                           
                                           [self.navigationController popViewControllerAnimated:YES];
                                           
                                           [[NSNotificationCenter defaultCenter] postNotificationName:OrderRefresh object:nil userInfo:nil];
                                       }
                                   } failure:^(NSString *err) {
                                       [SVProgressHUD showErrorWithStatus:err];
                                   }];
    
}

- (void)cancelOrder {
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您确定取消该订单吗？" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *doneAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self cancelOrderRequedt];
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    [alertVC addAction:doneAction];
    [alertVC addAction:cancelAction];
    
    [self presentViewController:alertVC animated:YES completion:nil];
}

- (void)cancelOrderRequedt {
    NSString *url = @"Orders/CancelOrder";
    
    [JNetManager cancelOrderRequedtUrl:url
                          waitPatModel:self.waitPayModel
                               success:^(id response) {
                                   if ([response integerValue] == 1) {
                                       [SVProgressHUD showSuccessWithStatus:@"取消成功"];
                                       [self.navigationController popViewControllerAnimated:YES];
                                       
                                       [[NSNotificationCenter defaultCenter] postNotificationName:OrderRefresh object:nil userInfo:nil];
                                   }
                               } failure:^(NSString *err) {
                                   [SVProgressHUD showErrorWithStatus:err];
                               }];
}

- (void)leftButtonClick:(UIButton *)leftButton
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
